package easyexcel.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.assertj.core.util.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * describe:
 *
 * @author huxingjie
 * @date 2019/09/11
 * @since 4.0
 */
public class DemoDataListener extends AnalysisEventListener<ExcelCameraViperVo> {
    private static final Logger log = LoggerFactory.getLogger(DemoDataListener.class);

    List<ExcelCameraViperVo> list = Lists.newArrayList();

    List<ExcelHandleError> error = Lists.newArrayList();

    ExcelConvertResponse<ExcelCameraViperVo> excelConvertResponse;

    public DemoDataListener(ExcelConvertResponse excelConvertResponse) {
        this.excelConvertResponse = excelConvertResponse;
    }
//    @Override
//    public void invoke(ExcelCameraViperVo excelCameraViperVo, AnalysisContext analysisContext) {
//        log.info("解析到一条数据:{}", JSON.toJSONString(excelCameraViperVo));
//        list.add(T);
//    }

    @Override
    public void onException(Exception exception, AnalysisContext context) {
        int row = context.readRowHolder().getRowIndex();
        log.error("解析失败，但是继续解析下一行", exception);
        error.add(new ExcelHandleError("", row + "某行格式有误", row + "格式有误"));
    }

    @Override
    public void invoke(ExcelCameraViperVo t, AnalysisContext analysisContext) {
        log.info("解析到一条数据:{}", JSON.toJSONString(t));
        list.add(t);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        excelConvertResponse.setSucceessList(list);
        excelConvertResponse.setErrorResultList(error);
        excelConvertResponse.setSize(list.size() + error.size());
        log.info("所有数据解析完成！");
    }

    public List<ExcelCameraViperVo> getList() {
        return list;
    }
}

    